<?php
    class Admin_Model_Newscomment extends Zend_Db_Table_Abstract
    {
        protected $_name = 'news_comment';
        protected $_primary = 'news_comment_id';
        protected $_db;
        
        public function __construct(){
            $this->_db = Zend_Registry::get('db');
        }
        public function listnews_comment(){
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->order('news_comment_id DESC');
            return $sql;
        }
        public function listnews_comment_resuilt(){
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->order('news_comment_id DESC');
            $result = $this->_db->fetchAll($sql);
            return $result;
            
        }
        public function getnews_commentById($news_comment_id){
            $where = $this->getAdapter()->quoteInto('news_comment_id = ?', $news_comment_id);
            $sql = $this->_db->select()
                                ->from($this->_name)
                                ->where($where);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        public function getnews_commentByName($news_id){
            $where = $this->getAdapter()->quoteInto('news_comment_id = ?', $news_comment_id);
            $sql = $this->_db->select()
                                ->from($this->_name)
                                ->where('news_comment_id = ?', $cat_id);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        public function checknews_comment($cat_name, $cat_id = ''){
			if($cat_id != ''){
				$sql = $this->_db->select()
								->from($this->_name)
                                ->where('news_comment_id != ?', $cat_id);
                                
				$result = $this->_db->fetchAll($sql);
				return count($result);
			}
			else{
				$sql = $this->_db->select()
								->from($this->_name)
								->where('news_comment_id = ?', $cat_name);
				$result = $this->_db->fetchAll($sql);
				return count($result);
			}
		}
        public function editnews_comment($data, $news_comment_id){
            $where = $this->getAdapter()->quoteInto('news_comment_id = ?',$news_comment_id);
            $this->_db->update($this->_name, $data, $where);
        }
        
        public function deletenews_comment($news_comment_id){
            $where = $this->getAdapter()->quoteInto('news_comment_id = ?',$news_comment_id);
            $this->_db->delete($this->_name, $where);
            $index = Zend_Search_Lucene::open('files/newscomment');
            $hits = $index->find('news_comment_id:' . $news_comment_id);           
            foreach($hits as $hit){
                $index->delete($hit->id);
            }
            $index->commit();
            $index->optimize();
        }
        
        public function deletenews_comments($listId){
            $index = Zend_Search_Lucene::open('files/cat');
            foreach ($listId as $value){
                $where = $this->getAdapter()->quoteInto('news_comment_id = ?', $value);
                $this->_db->delete($this->_name, $where);
                $hits = $index->find('news_comment_id:' . $value);
                foreach($hits as $hit){
                    $index->delete($hit->id);
                }
			}
            $index->commit();
            $index->optimize();
        }
        
        public function searchnews_comment($keyword){
            Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('utf-8');
            setlocale(LC_CTYPE, 'UTF-8');
            Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
			$index = Zend_Search_Lucene::open('files/newscomment');
            if(!empty($keyword)){
                $hits = $index->find($keyword);
                return $hits;
            }
		}
        
        public function saveAddItem($arrParam = null, $options = null){
            
            if($options['task'] == 'edit')
            {
                try
                {
                    Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8');
                    setlocale(LC_CTYPE, 'UTF-8');
                    Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
                    $index = Zend_Search_Lucene::open('files/newscomment');
                    Zend_Search_Lucene::setResultSetLimit(350);
                }
                catch(exception $e)
                {
                    Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8');
                    setlocale(LC_CTYPE, 'UTF-8');
                    Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
                    $index = Zend_Search_Lucene::create('files/newscomment');
                }
                
                $hits = $index->find('news_comment_id:'.$arrParam['news_comment_id']);
                foreach($hits as $hit)
                {
                    $index->delete($hit->id);
                }
                
                $doc = new Zend_Search_Lucene_Document();
                $doc->addField(Zend_Search_Lucene_Field::keyword('news_comment_id', $arrParam['news_comment_id']))
                    ->addField(Zend_Search_Lucene_Field::text('user_id', $arrParam['user_id'], 'UTF-8'))
                    ->addField(Zend_Search_Lucene_Field::text('news_id', $arrParam['news_id'], 'UTF-8'))
                    ->addField(Zend_Search_Lucene_Field::text('status', $arrParam['status']))
                    ->addField(Zend_Search_Lucene_Field::text('create_date', $arrParam['create_date']))
                    ->addField(Zend_Search_Lucene_Field::text('news_comment_content', $arrParam['news_comment_content'], 'UTF-8'));
                
                $index->addDocument($doc);
                $index->commit();
                $index->optimize();
            }
        }
        function truncateString($str, $len, $charset="UTF-8"){
        $str = html_entity_decode($str, ENT_QUOTES, $charset);   
        if(mb_strlen($str, $charset)> $len){
            $arr = explode(' ', $str);
            $str = mb_substr($str, 0, $len, $charset);
            $arrRes = explode(' ', $str);
            $last = $arr[count($arrRes)-1];
            unset($arr);
            if(strcasecmp($arrRes[count($arrRes)-1], $last))   unset($arrRes[count($arrRes)-1]);
          return implode(' ', $arrRes)."...";   
       }
        return $str;
        }
    }
